package com.jayqqaa12.robot.lottery;

import java.util.Iterator;
import java.util.List;

import weiboclient4j.StatusService;
import weiboclient4j.UserService;
import weiboclient4j.WeiboClient;
import weiboclient4j.WeiboClientException;
import weiboclient4j.model.Status;
import weiboclient4j.params.P;
import weiboclient4j.params.Paging;

import com.google.common.collect.Lists;
import com.jayqqaa12.robot.Consts;
import com.jayqqaa12.robot.model.Lottery;
import com.jayqqaa12.robot.model.Weibo;

public class SinaService {

	public static WeiboClient getClient(String token) {

		WeiboClient client = new WeiboClient(Consts.SINA_APPID, Consts.SINA_APPSERECT);
		client.setAccessToken(token);

		return client;
	}

	public static Long getUid(String nickname, String token) {

		UserService us = new UserService(getClient(token));

		try {
			
			return us.show(P.screenName(nickname)).getId();
		} catch (WeiboClientException e) {
			e.printStackTrace();

			return null;
		}

	}

	public static List<Status> statusList(String token) {

		StatusService ss = new StatusService(getClient(token));

		try {
			
			return ss.getUserTimeline( Paging.create().count(50).page(1)).getStatuses();

		} catch (WeiboClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return Lists.newArrayList();
	}

	public static List<Weibo> filterLottyeStatus(List<Status> list, String uid) {
		
		
		List<Weibo> lotteryList = Weibo.dao.list("where uid=?",uid);
		
		Iterator<Status> iter =list.iterator();
		
		while(iter.hasNext()){
			Status s = iter.next();
			for(Weibo w :lotteryList){
				if(w.getLong("sid").equals(s.getId())){
					
					w.put("status", s);
					w.putModel("lottery", Lottery.dao.list(" where sid =?",s.getId()));

					iter.remove();
					break;
				}
			}
		}
		
		
		return lotteryList;
		
	}

	
	
	
	
	
	
	
	
	
	
	
	
}
